class Solution:
    def nextPermutation(self, nums):
        """
        Do not return anything, modify nums in-place instead.
        """
        len_num = len(nums)
        if len_num == 1:
            return
        i = len_num - 1
        while i >= 0:
            if nums[i] <= nums[i-1]:
                i -= 1
            elif i == len_num - 1:
                nums[i], nums[i-1] = nums[i-1], nums[i]
                return
            elif i == 0:
                nums.sort()
                return
            else:
                i -= 1
                break
        j = i + 1
        while j < len_num - 1 and nums[j+1] > nums[i]:
            j += 1
        nums[i], nums[j] = nums[j], nums[i]
        h = len_num - 1
        i += 1
        while i < h:
            nums[i], nums[h] = nums[h], nums[i]
            i += 1
            h -= 1


a = Solution()
b = [5,1,1]
a.nextPermutation(b)
print(b)
